/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.Exercise.listExercise;

import com.myapp.model.estados.Estados;
import com.myapp.model.notificaciones.Notificaciones;
import com.myapp.model.tipos.Tipos;
import com.myapp.model.usuarios.Usuarios;
import com.myapp.svc.Exercise.ExerciseSvc;
import com.myapp.svc.Notifications.NotificationsSvc;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author CarmaZone
 */
public class myExercisesAction extends org.apache.struts.action.Action {

    /*
     * forward name="success" path=""
     */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String noti = request.getParameter("noti");

        if (noti != null && noti.equals(noti)) {
            List<Notificaciones> notiList = new ArrayList();
            HttpSession session = request.getSession();
            Usuarios usr = new Usuarios();
            Tipos tip = new Tipos();

            usr = (Usuarios) session.getAttribute("usuario");
            NotificationsSvc notiSvc = new NotificationsSvc();
            notiList = notiSvc.listNotifications(usr);

            for (Notificaciones not : notiList) {
                if (not.getTipos().getIdtipo() == Integer.valueOf("6")) {
                    tip.setIdtipo(Integer.valueOf("6"));
                } else {
                    tip.setIdtipo(Integer.valueOf("7"));
                }
                notiSvc.changeStateNotifications(notiList, tip);
            }

            ArrayList al = (ArrayList) session.getAttribute("contador");
            al.set(2, "0");
            session.setAttribute("contador", al);
        }

        ExerciseSvc myExerc = new ExerciseSvc();
        List noResueltos = new ArrayList();
        List noCorregidos = new ArrayList();


        //Cogemos el usuario de sesión
        HttpSession session = request.getSession();
        Usuarios user = null;
        user = (Usuarios) session.getAttribute("usuario");

        noResueltos = myExerc.listEjerNoResueltos(user.getIdusuario());
        noCorregidos = myExerc.listEjerNoCorregidos(user.getIdusuario());

        session.setAttribute("noResueltos", noResueltos);
        session.setAttribute("noCorregidos", noCorregidos);
        return mapping.findForward(SUCCESS);
    }
}
